/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.modules.order.order.mapper;

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.order.order.entity.domain.YxStoreOrder;
import co.yixiang.modules.shop.entity.dto.ChartDataDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author hupeng
 * @date 2020-05-12
 */
@Repository
@Mapper
public interface StoreOrderMapper extends CoreMapper<YxStoreOrder> {

    @Select("SELECT COUNT(*) FROM yx_store_order WHERE payment_time >= ${today}")
    Integer countByPayTimeGreaterThanEqual(@Param("today") int today);

    @Select("SELECT COUNT(*) FROM yx_store_order WHERE payment_time < ${today}  and payment_time >= ${yesterday}")
    Integer countByPayTimeLessThanAndPayTimeGreaterThanEqual(@Param("today") int today, @Param("yesterday") int yesterday);

    @Select("select IFNULL(sum(pay_price),0)  from yx_store_order " +
            "where order_status!='REFUND' and is_del=0 and pay_status='PAID'")
    Double sumTotalPrice();

    //修复了“错误代码1055与sql_mode = only_full_group_by不兼容”问题
    //修复方式create_time外面包裹UNIX_TIMESTAMP关键字
    @Select("SELECT IFNULL(sum(pay_price),0) as num," +
            "FROM_UNIXTIME(UNIX_TIMESTAMP(create_time), '%m-%d') as time " +
            " FROM yx_store_order where order_status!='REFUND' and is_del=0 and pay_status='PAID' and payment_time >= ${time}" +
            " GROUP BY FROM_UNIXTIME(UNIX_TIMESTAMP(create_time),'%Y-%m-%d'),FROM_UNIXTIME(UNIX_TIMESTAMP(create_time), '%m-%d') " +
            " ORDER BY time ASC")
    List<ChartDataDto> chartList(@Param("time") int time);

    

    @Select("SELECT count(id) as num," +
            "FROM_UNIXTIME(UNIX_TIMESTAMP(create_time), '%m-%d') as time " +
            " FROM yx_store_order where order_status!='REFUND' and is_del=0 and pay_status='PAID' and payment_time >= ${time}" +
            " GROUP BY FROM_UNIXTIME(UNIX_TIMESTAMP(create_time),'%Y-%m-%d'),FROM_UNIXTIME(UNIX_TIMESTAMP(create_time), '%m-%d') " +
            " ORDER BY time ASC")
    List<ChartDataDto> chartListT(@Param("time") int time);


    @Select("SELECT yso.uid 'key', SUM(ysoi.num) 'value' FROM yx_store_order yso INNER JOIN yx_store_order_item ysoi ON yso.sn = ysoi.order_sn WHERE ysoi.goods_id = #{goodId} AND yso.order_type = #{orderType} GROUP BY yso.uid;")
    List<Map<String,Object>> selectYxUserByAdoptAndGoodId(@Param("goodId") String goodId,@Param("orderType") String orderType);

    /**
     * 查询用户下，不同订单状态的总数
     * @param uid
     * @return  order_status,size
     */
    @Select("SELECT order_status, COUNT(id) 'size' FROM yx_store_order WHERE uid = #{uid} GROUP BY order_status")
    List<Map<String,Object>> selectOrderStatusSizeByUid(@Param("uid") String uid);
}
